package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @author 23333
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO) {
        log.info(dishDTO.toString());

        dishService.saveWithFlavor(dishDTO);
        String pattern = "dish_" + dishDTO.getCategoryId();
        cleanCache(pattern);

        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> Page(DishPageQueryDTO dishPageQueryDTO) {
        log.info(dishPageQueryDTO.toString());
        PageResult pageResult = dishService.pageDish(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam String[] ids) {
        log.info("删除的ids:{}", Arrays.toString(ids));
        dishService.deleteBatch(ids);
        //将所有的菜品缓存数据清理，所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 菜品启售停售
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("status/{status}")
    @ApiOperation("菜品启售停售")
    public Result startOrStop(@PathVariable String status, String id) {
        log.info("status:{},id:{}", status, id);
        dishService.dishOpenOrClose(status, id);

        //将所有的菜品缓存数据清理，所有以dish_开头的key
        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> queryDishById(@PathVariable Long id) {
        DishVO dishVO = dishService.findDishWithFlavorsById(id);
        return Result.success(dishVO);
    }

    /**
     * 更新菜品和口味数据
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("更新菜品和口味数据")
    public Result updateDishWithFlavor(@RequestBody DishDTO dishDTO) {
        dishService.updateDishWithFlavorsById(dishDTO);
        log.info("dishDto:{}", dishDTO.toString());

        //将所有的菜品缓存数据清理，所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 动态条件查询菜品
     *
     * @param dish
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> listDish(Dish dish) {
        List<Dish> dishList = dishService.list(dish);
        return Result.success(dishList);
    }


    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
